//
//  BKHuanJingLayout.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/6/8.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKHuanJingLayout.h"


@interface BKHuanJingLayout (){

    NSInteger _totalNumber;
    CGRect _rect;
}

@end
@implementation BKHuanJingLayout

- (void)prepareLayout{
    [super prepareLayout];
    _totalNumber =  [self.collectionView numberOfItemsInSection:0];
    _rect = self.collectionView.frame;
    
    
}



- (CGSize)collectionViewContentSize
{
    return CGSizeMake(_rect.size.width, (_totalNumber/3+1)*(_rect.size.width-30)/3+( _totalNumber/3-1)*10+30);
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    NSInteger col = indexPath.row%3 ;
    NSInteger row = indexPath.row/3;
    
    
    
    attributes.center = CGPointMake(5+(_rect.size.width-20)/6+col*(_rect.size.width)/3,(_rect.size.width)/6+row*((_rect.size.width-30)/3+10));
    
    attributes.size = CGSizeMake((_rect.size.width-30)/3,(_rect.size.width-30)/3);
    
    
    
    return attributes;
}

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSArray *arr = [super layoutAttributesForElementsInRect:rect];
    if ([arr count] > 0) {
        return arr;
    }
    NSMutableArray *attributes = [NSMutableArray array];
    for (NSInteger i = 0 ; i < [self.collectionView numberOfItemsInSection:0 ]; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        [attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
    }
    return attributes;
}

@end
